﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using Microsoft.Phone.Controls;
using System.Device.Location;
using Microsoft.Phone.Controls.Maps;

namespace $safeprojectname$
{
    public partial class MapPage : PhoneApplicationPage
    {
        GeoLocationHelper geoHelper;

        public MapPage()
        {
            InitializeComponent();
            this.Loaded += new RoutedEventHandler(MapPage_Loaded);
        }

        void MapPage_Loaded(object sender, RoutedEventArgs e)
        {
            try
            {
                geoHelper = new GeoLocationHelper(GeoPositionAccuracy.High, 0.5);
                geoHelper.StatusChanged +=new GeoLocationHelper.GeoPositionStatusChangedEventHandler(geoHelper_StatusChanged);
                geoHelper.LocationChanged +=new GeoLocationHelper.GeoPositionLocationChangedEventHandler(geoHelper_LocationChanged);
                LoadMapItems();
            }
            catch (Exception ex)// Handle exception?
            {
                UpdateStatus("Error retrieving map data. Please check internet connection or make sure device location service is enabled.");
            }
        }

        void geoHelper_LocationChanged(object sender, GeoPositionChangedEventArgs<GeoCoordinate> e)
        {
            if (geoHelper.Location != null)
            {
                mpNearby.SetView(geoHelper.Location, 15);
            }
        }

        void geoHelper_StatusChanged(object sender, GeoPositionStatusChangedEventArgs e)
        {
            string currentStatus = "";

            switch (e.Status)
            {
                case GeoPositionStatus.Disabled:
                    // The location service is disabled or unsupported.
                    // Alert the user
                    currentStatus = "Location service is disabled or unsupported";
                    break;
                case GeoPositionStatus.Initializing:
                    // The location service is initializing.
                    // Disable the Start Location button
                    currentStatus = "looking for you…";
                    break;
                case GeoPositionStatus.NoData:
                    // The location service is working, but it cannot get location data
                    // Alert the user and enable the Stop Location button
                    currentStatus = "can’t find you yet…";
                    break;
                case GeoPositionStatus.Ready:
                    // The location service is working and is receiving location data
                    // Show the current position and enable the Stop Location button
                    currentStatus = "found you!";
                    break;
            }
            UpdateStatus(currentStatus);
        }

        #region MapItemManager
        private void LoadMapItems()
        {
            AddHerePushpin(geoHelper.Location);
            UpdateStatus("Loading items.....");

            //Create items that contain the GeoLocation of your items that are to be displayed in the map
            //foreach (YourItem si in YourItems)
            //{
            //    Pushpin p = new Pushpin();
            //    p.Location = new GeoCoordinate(si.GeoLocation.Latitude, si.GeoLocation.Longitude);
            //    p.Content = si.AugmentName + Environment.NewLine + si.Description;
            //    p.MouseLeftButtonDown += new MouseButtonEventHandler(p_MouseLeftButtonDown);
            //    mpNearby.Children.Add(p);
            //}

            mpNearby.UpdateLayout();
            UpdateStatus("");

        }
        private void AddHerePushpin(GeoCoordinate here)
        {
            MapLayer imageLayer = new MapLayer();

            Image image = new Image();
            //Define the URI location of the image
            image.Source = new System.Windows.Media.Imaging.BitmapImage(new Uri("Images\\YouAreHere.png", UriKind.Relative));
            //Define the image display properties
            image.Opacity = 0.8;
            image.Stretch = System.Windows.Media.Stretch.None;

            //Center the image around the location specified
            PositionOrigin position = PositionOrigin.Center;

            //Add the image to the defined map layer
            imageLayer.AddChild(image, here, position);
            //Add the image layer to the map
            mpNearby.Children.Add(imageLayer);
        }

        void p_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
        {
            Pushpin p = (Pushpin)sender;
            //Get data from the pushpin pressed on map and handle event
        }
        #endregion MapItemManager

        #region HelperMethods
        private void UpdateStatus(string status)
        {
            txtStatus.Text = status;
            txtStatus.UpdateLayout();
        }
        #endregion HelperMethods
    }
}